package com.app.aipass.fragment.my.reward;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.activity.MainActivity;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.FragmentRewardBinding;
import com.app.aipass.entity.GetRewardEntity;
import com.app.aipass.utils.DoubleUtils;
import java.util.ArrayList;
import me.goldze.mvvmhabit.base.BaseFragment;

public class RewardFragment extends BaseFragment<FragmentRewardBinding, RewardViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_reward;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RewardViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(RewardViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        viewModel.requestNetWork();
        viewModel.mGetRewardEntity.observe(this, new Observer<GetRewardEntity>() {
            @Override
            public void onChanged(GetRewardEntity getRewardEntity) {
                RewardAdapter adapter = new RewardAdapter(R.layout.item_reward_list, getRewardEntity.getReward_orders());
                binding.mRecyclerView.setAdapter(adapter);
                binding.mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));

                ArrayList<GetRewardEntity.RewardOrdersBean> list = getRewardEntity.getReward_orders();
                double income = 0;
                //释放中 1
                double noRelease = 0;
                //冻结中 3
                double noReleaseAll = 0;
//                double noRelease = 0;
                for (int i = 0; i < list.size(); i++) {
                    income = DoubleUtils.add(income, Double.valueOf(list.get(i).getAcc_income()));
                    if (list.get(i).getStatus() == 1) {
                        noRelease = DoubleUtils.add(noRelease, Double.valueOf(list.get(i).getNo_release()));
                    }
                    if (list.get(i).getStatus() == 3) {
                        noReleaseAll = DoubleUtils.add(noReleaseAll, Double.valueOf(list.get(i).getNo_release()));
                        Log.i("213213",""+noReleaseAll);
                    }
                }
                binding.tvOverNum.setText(income + "");

                binding.tvNotUseNum.setText(noReleaseAll + "");

                binding.tvNowNotNum.setText(noRelease + "");
            }
        });

    }
}
